<?php


namespace catchAdmin\contract\controller;

use app\common\model\PatientInfo;
use catchAdmin\contract\request\CommunityRequest;
use catchAdmin\contract\request\ContractRequest;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use catcher\Code;
use catcher\exceptions\CatchException;
use catcher\exceptions\FailedException;
use catcher\exceptions\ValidateFailedException;
use catcher\generate\factory\Controller;
use think\exception\ValidateException;

class Contract extends Controller
{
    protected $domain = null;

    public function __construct()
    {
        $this->domain = new \app\common\business\Contract();
    }

    public function index(CatchRequest $request)
    {
        $postData["name"] = $request->param("name", "", "string");
        $postData["status"] = $request->param("status", "", "string");
        $postData["groupId"] = $request->param("group_id", "", "string");
        $result = (new \app\common\model\Contract())->getLists($postData);
        $result = array_merge($result, ["code" => Code::SUCCESS, "message" => "success"]);
        return json($result);

    }


    public function save(CatchRequest $request)
    {
        $postData = $request->param();
        try {
            validate(ContractRequest::class)->scene("create")->check($postData);
            //执行创建签约
            $result = $this->domain->createData($postData);
            return CatchResponse::success(["insertId" => $result]);
        } catch (ValidateException $exception) {
            throw new ValidateFailedException($exception->getMessage());
        }
    }

    public function update(CatchRequest $request, string $id): \think\response\Json
    {
        $postData = $request->only(["notice_type", "contract_doctor", "inform_doctor", "status", "service_info", "updated_at", "deleted_at", "attachment", "termination_reason"], "put");
        $result = $this->domain->modifyData($id, $postData);
        if (!$result) {
            throw new FailedException("更新失败");
        }
        return CatchResponse::success([], "更新成功");
    }

    public function getContractListByPatient(CatchRequest $request): \think\response\Json
    {
        $patientId = $request->param("patient_id", "", "string");
        if (empty($patientId)) {
            throw new FailedException("患者不存在");
        }
        //根据患者ID获取数据列表
        $result = $this->domain->getListByPatientId($patientId);

        return CatchResponse::paginate($result);
    }

    public function attention(CatchRequest $request)
    {
        $id = $request->param("id", "", "string");
        if (empty($id)) {
            throw new FailedException("请传入ID");
        }
        $type = $request->param("type", 1, "int");
        $result = $this->domain->attention($request->user()->id, $id, $type);
        if ($result->getModifiedCount() > 0) {
            return CatchResponse::success();
        }
    }

    public function signingGroup(CatchRequest $request): \think\response\Json
    {
        $patientList = $request->param("patient_list", []);
        $sourceGroupId = $request->param("source_group_id", "", "string");
        $groupId = $request->param("group_id", "", 'string');
        $type = $request->param("type", 2, "int");
        $isAll = $request->param("is_all", false, "boolean");

        if (empty($patientList) && $isAll === false) {
            throw new FailedException("患者列表不存在");
        }
        if ($isAll === true && $sourceGroupId !== "") {
            if ($sourceGroupId === "-2") {
                $sourceGroupId = "NO";
            }
            if ($sourceGroupId === "-1") {
                $sourceGroupId = "ALL";
            }

            $patientIds = (new PatientInfo())->getAllPatientIds($sourceGroupId, $request->user()['id']);
            if (empty($patientIds)) {
                throw new FailedException("没有患者被选中");
            }
            $patientList = (new PatientInfo())->where("patientId", "in", $patientIds)->field(['patient_uuid'=>['$toString'=>'$_id'],'patient_id'=>'$patientId','name','sex'])->select()->hidden(["id"])->toArray();

        }

        $result = $this->domain->grouping($patientList, $groupId, $request->user()['id'], $type);
        if ($result) {
            return CatchResponse::success(["group_id" => $groupId]);
        }
        return CatchResponse::fail("分组失败");

    }


}